#include "vxzoomslider.h"
#include "vxmovementstate.h"
#include "vxmousetransmitter.h"
#include "Vx.h"


VxZoomSlider::VxZoomSlider(VxZoomController * zoomController, QWidget * parent) :
		QFrame::QFrame(parent), _zoomController(zoomController), _scaled(false)
{
	setFocusPolicy(Qt::NoFocus);

	setGeometry(100, 170, 60, 150);

	setFixedSize(60,150);

	QVBoxLayout * vlayout = new QVBoxLayout();
	this->setLayout(vlayout);
	
	QPushButton * zoomInButton = new QPushButton();
	QPushButton * zoomOutButton = new QPushButton();

	zoomInButton->setFocusPolicy(Qt::NoFocus);
	zoomOutButton->setFocusPolicy(Qt::NoFocus);

	connect(zoomInButton, SIGNAL(clicked()), this, SLOT(_zoomInClicked()));
	connect(zoomOutButton, SIGNAL(clicked()), this, SLOT(_zoomOutClicked()));

	_slider = new QSlider(Qt::Vertical);
	_slider->setFocusPolicy(Qt::NoFocus);

	QHBoxLayout * hlayout = new QHBoxLayout();
	vlayout->addLayout(hlayout);
	hlayout->addWidget(zoomInButton);

	hlayout = new QHBoxLayout();
	vlayout->addLayout(hlayout);
	hlayout->addWidget(_slider);

	hlayout = new QHBoxLayout();
	vlayout->addLayout(hlayout);
	hlayout->addWidget(zoomOutButton);


	VxMovementState * movement = new VxMovementState(this);
	connect(movement, SIGNAL(movement(int,int)), this, SLOT(_movement(int,int)));
	QStateMachine * machine = new QStateMachine();
	machine->addState(movement);
	machine->setInitialState(movement);
	machine->start();

	_slider->setMinimum(1);
	_slider->setMaximum(30);
	_slider->setValue(1);

	_slider->setPageStep(5);
	_slider->setSingleStep(5);

	connect(_slider, SIGNAL(sliderMoved(int)), this, SLOT(_sliderMoved(int)));
	connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(_valueChanged(int)));
	connect(_zoomController, SIGNAL(scaleShanged(int)), this, SLOT(_setValue(int)));

	this->setObjectName("vxZoomSliderWidget");
	_slider->setObjectName("vxZoomSlider");
	zoomInButton->setObjectName("vxZoomSliderButtonIn");
	zoomOutButton->setObjectName("vxZoomSliderButtonOut");


}


void VxZoomSlider::_zoomInClicked()
{
	int s = (int(_slider->value()/5) + 1)*5;
	_zoomController->scaleToView(s);
}


void VxZoomSlider::_zoomOutClicked()
{
	int v = _slider->value();

	int s = (v%5 == 0) ? (int(v/5) - 1)*5 : int(v/5)*5;

	if (s < 1){
		s = 1;
	}
	_zoomController->scaleToView(s);
}


void VxZoomSlider::_movement(int mx, int my)
{
	qDebug() << mx << " " << my;
}


void VxZoomSlider::_sliderMoved(int scale)
{
	if (_scaled){
		_slider->setValue(_scale);
		return;
	}
	_scaled = true;
	_scale = scale;
	_zoomController->scaleTo(scale);
	_scaled = false;
}




void VxZoomSlider::_valueChanged(int scale)
{
	if (_scaled){
		return;
	}

	_zoomController->scaleToView(scale, false);

}



void VxZoomSlider::_setValue(int scale)
{
	if (_slider->isSliderDown()){
		return;
	}
	_scaled = true;

	_slider->setValue(scale);

	setToolTip(QString("%1\%").arg(int(scale*100)));

	_scaled = false;

}


